/*
Java board game library.
Copyright (C) 2011  Deepesh Garg

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package d.g.boardgames ;

import java.util.* ;


public class MoveHelper {

	public static List<Ray> invertRayList (List<Ray> rayList) {
		List<Ray> invertedRayList = new ArrayList<Ray>() ;
		for (Ray ray: rayList) {
			Ray invertedRay = invertRay(ray) ;
			invertedRayList.add(invertedRay) ;
		}
		return invertedRayList ;
	}

	public static Ray invertRay(Ray ray) {
		Ray invertedRay = new Ray() ;
		for (Cell cell: ray.getCells()) {
			Cell invertedCell = new Cell (-cell.getRow(), -cell.getCol()) ;
			invertedRay.addCell(invertedCell) ;
		}
		return invertedRay ;
	}

	public static List<Cell> getAllowedEmpty (Board board, List<Ray> rayList, Cell thisCell) {
		int maxRows = board.getRowCount() - 1 ;
		int maxCols = board.getColCount() - 1;
		List<Cell> allowedCells = new ArrayList<Cell>() ;
		for (Ray ray: rayList) {
			for (Cell cell: ray.getCells()) {
				int row = thisCell.getRow() + cell.getRow() ;
				int col = thisCell.getCol() + cell.getCol() ;
				if (row < 0 || row > maxRows) {
					break ;
				}
				if (col < 0 || col > maxCols) {
					break ;
				}
				if (board.getCellState(row, col) != null) {
					break ;
				}
				allowedCells.add(new Cell(row, col)) ;
			}
		}
		return allowedCells ;
	}

	public static List<Cell> getAllowedNonEmpty (Board board, List<Ray> rayList, Cell thisCell) {
		int maxRows = board.getRowCount() - 1 ;
		int maxCols = board.getColCount() - 1;
		List<Cell> allowedCells = new ArrayList<Cell>() ;
		for (Ray ray: rayList) {
			for (Cell cell: ray.getCells()) {
				int row = thisCell.getRow() + cell.getRow() ;
				int col = thisCell.getCol() + cell.getCol() ;
				if (row < 0 || row > maxRows) {
					break ;
				}
				if (col < 0 || col > maxCols) {
					break ;
				}
				if (board.getCellState(row, col) != null) {
					allowedCells.add(new Cell(row, col)) ;
					break ;
				}
			}
		}
		return allowedCells ;
	}
}
